package com.cqs.leetcode.math;

import com.cqs.leetcode.TestCaseUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lixiaowen
 * @create 2020-01-06
 */
public class PlusOne66 {

    static class Solution {


        public int[] plusOne(int[] digits) {
            //1. 考虑进位 89
            int idx = digits.length;
            int p = 1;
            while (--idx >= 0 && p == 1) {
                int tmp = digits[idx] + p;
                p = tmp / 10;
                digits[idx] = tmp % 10;
            }
            //最高位[之前]没有进位
            if (p == 0) return digits;
            int[] result = new int[digits.length + 1];
            System.arraycopy(digits, 0, result, 1, digits.length);
            result[0] = 1;
            return result;

        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        List<int[]> cases = new ArrayList<>();
        cases.add(new int[]{0});
        cases.add(new int[]{9, 9});
        cases.add(new int[]{8, 9});
        cases.stream().forEach(it -> {
            System.out.println(TestCaseUtils.arrFormat(it) + " \t" + TestCaseUtils.arrFormat(solution.plusOne(it)));
        });
    }
}
